<?php
/* Kapcsolat kezelése az MSN kiszolgálóval
 */ 

class MSN_Connection {
    private $server             = '';
    private $port               = 0;
    public $server_connection  = false;
    public $stream_timeout     = 1;
    public  $trans_id           = 0;
    public  $helper              = null;


    public function __construct( $parent ) {
        $this->trans_id = 1;
        $this->server   = 'messenger.hotmail.com';
        $this->port     = 1863;

        $this->helper   = &$parent->helper;
    } // end func

    //Kapcsolat létrehozása
    public function init( $server = '', $port = '' ) {
        if ( $this->server_connection )
            $this->close();

        if ($server == '')
            $server = $this->server;

        if ($port == '')
            $port = $this->port;

        $this->server_connection = @fsockopen($server, $port, $errno, $errstr, 5);
        if (!$this->server_connection) {
            die(sprintf('Error: Can\'t connect to %s:%s, error => %s, %s', $server, $port, $errno, $errstr));
        }

        stream_set_timeout($this->server_connection, $this->stream_timeout);
        $this->WriteLn('VER', 'MSNP15 CVR0');
    } // end func

    //Kapcsolat lezárása
    public function close() {
        $this->helper->debug('Closing NS connection');
        fclose($this->server_connection);        
    } // end func

    //Szakadt-e a kapcsolat
    public function EOF() {
        return feof($this->server_connection);
    } // end func

    //Sor küldése a szervernek
    public function WriteLn( $cmd, $data ) {
        $fullCmd = sprintf( '%s %d %s', $cmd, $this->trans_id, $data );

        @fwrite($this->server_connection, $fullCmd."\r\n");
        $this->helper->debug('NS: >>> '.$fullCmd);
        $this->trans_id++;
        return;        
    } // end func

    //Adat küldése a szervernek
    public function WriteData( $data ) {
        @fwrite($this->server_connection, $data);
        $this->helper->debug('NS: >>> '.$data);
        return;
    }
    
    //Sor olvasása a szerverről
    public function ReadLn() {
        $data = @fgets($this->server_connection, 4096);
        if ($data !== false) {
            $data = trim($data);
            $this->helper->debug('NS: <<< '.$data);
        }
        return $data;
    }

    //Adat olvasása a szerverről
    public function ReadData($size, $hide = false) {
        $data = '';
        $count = 0;

        while ( !$this->EOF() ) {
            $buf = @fread($this->server_connection, $size - $count);
            $data .= $buf;
            $count += strlen($buf);
            if ( $count >= $size ) break;
        }
    
        if ( !$hide )
            $this->helper->debug( sprintf('NS: <<< data (%d/%d) <<<\n%s', $size, $count, $data ));

        return $data;
    }

    //Timeout kezelése
    function handleTimeout( $start_ts, $timeout ) {
        $now_ts = time();
        $used_time = ($now_ts >= $start_ts) ? $now_ts - $start_ts : $now_ts;

        if ($timeout > 0 && $used_time > $timeout) {
            $this->helper->logoutError('Timeout, maybe protocol changed!');
            return false;
        }
    } // end func    
} // end class

 
?>